﻿
using System;
using System.Collections.Generic;
using System.Data;
using QLKS.Entity;
using System.Text;
using System.Linq;

namespace QLKS.DAL
{
    public class PhieuThuePhongDAO
    {
        private SqlDataProvider dataProvider;

        public PhieuThuePhongDAO()
        {
            dataProvider = new SqlDataProvider();
        }

        public List<PhieuThuePhong> GetByAll(PhieuThuePhong obj)
        {
            List<PhieuThuePhong> lstData = new List<PhieuThuePhong>(); ;
            try
            {
                DataTable dt = dataProvider.ExecuteDataTable("sp_PhieuThuePhong_GetByAll"
                    , new string[] { "MaPhieuThuePhong", "PhongThue", "NgayThuePhong", "GhiChu", "ListDetail" }
                    , new object[] { obj.MaPhieuThuePhong, obj.PhongThueID, obj.NgayThuePhong, obj.GhiChu, ParseListToXML(obj.ListDetail) }
                );
                PhieuThuePhong entity;
                foreach(DataRow myrow in dt.Rows)
                {
                    entity = new PhieuThuePhong();
                    entity.MaPhieuThuePhong = Convert.ToInt32(myrow["MaPhieuThuePhong"]);
                    entity.PhongThueID = Convert.ToInt32(myrow["PhongThue"]);
                    entity.NgayThuePhong = myrow["NgayThuePhong"] is DBNull ? string.Empty : myrow["NgayThuePhong"].ToString();
                    entity.GhiChu = myrow["GhiChu"] is DBNull ? string.Empty : myrow["GhiChu"].ToString();
                    try
                    {
                        if (entity.PhongThueID != null && entity.PhongThueID != 0)
                        {
                            entity.PhongThue = ((new DMPhongDAO()).GetByAll(new DMPhong(entity.PhongThueID))).FirstOrDefault();
                        }
                        else
                        {
                            entity.PhongThue = null;
                        }
                    }
                    catch
                    {
                        entity.PhongThue = null;
                    }
                    try
                    {
                        if (entity.MaPhieuThuePhong != null && entity.MaPhieuThuePhong != 0)
                        {
                            entity.ListDetail = ((new ChiTietPhieuThuePhongDAO()).GetByAll(new ChiTietPhieuThuePhong(entity.MaPhieuThuePhong, 0)));
                        }
                        else
                        {
                            entity.ListDetail = null;
                        }
                    }
                    catch
                    {
                        entity.ListDetail = null;
                    }
                    lstData.Add(entity);
                }
                
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return lstData;
        }

        public bool Insert(PhieuThuePhong obj)
        {
            int i = dataProvider.ExecuteNonQuery("sp_PhieuThuePhong_Insert"
                    , new string[] { "MaPhieuThuePhong", "PhongThue", "NgayThuePhong", "GhiChu", "ListDetail" }
                    , new object[] { obj.MaPhieuThuePhong, obj.PhongThueID, obj.NgayThuePhong, obj.GhiChu, ParseListToXML(obj.ListDetail) }
                );
            return i == 1 ? true : false;
        }

        public bool Update(PhieuThuePhong obj)
        {
            int i = dataProvider.ExecuteNonQuery("sp_PhieuThuePhong_Update"
                    , new string[] { "MaPhieuThuePhong", "PhongThue", "NgayThuePhong", "GhiChu", "ListDetail" }
                    , new object[] { obj.MaPhieuThuePhong, obj.PhongThueID, obj.NgayThuePhong, obj.GhiChu, ParseListToXML(obj.ListDetail) }
                );
            return i == 1 ? true : false;
        }

        public bool Delete(PhieuThuePhong obj)
        {
            int i = dataProvider.ExecuteNonQuery("sp_PhieuThuePhong_Delete"
                    , new string[] { "MaPhieuThuePhong", "PhongThue", "NgayThuePhong", "GhiChu", "ListDetail" }
                    , new object[] { obj.MaPhieuThuePhong, obj.PhongThueID, obj.NgayThuePhong, obj.GhiChu, ParseListToXML(obj.ListDetail) }
                );
            return i == 1 ? true : false;
        }

        public List<PhieuThuePhong> GetPhongThue(PhieuThuePhong obj)
        {
            List<PhieuThuePhong> lstData = new List<PhieuThuePhong>(); ;
            try
            {
                DataTable dt = dataProvider.ExecuteDataTable("sp_PhieuThuePhong_GetPhongThue"
                    , new string[] { "MaPhieuThuePhong", "PhongThue", "NgayThuePhong", "GhiChu", "ListDetail" }
                    , new object[] { obj.MaPhieuThuePhong, obj.PhongThueID, obj.NgayThuePhong, obj.GhiChu, ParseListToXML(obj.ListDetail) }
                );
                PhieuThuePhong entity;
                foreach (DataRow myrow in dt.Rows)
                {
                    entity = new PhieuThuePhong();
                    entity.MaPhieuThuePhong = Convert.ToInt32(myrow["MaPhieuThuePhong"]);
                    entity.PhongThueID = Convert.ToInt32(myrow["PhongThue"]);
                    entity.NgayThuePhong = myrow["NgayThuePhong"] is DBNull ? string.Empty : myrow["NgayThuePhong"].ToString();
                    entity.GhiChu = myrow["GhiChu"] is DBNull ? string.Empty : myrow["GhiChu"].ToString();
                    try
                    {
                        if (entity.PhongThueID != null && entity.PhongThueID != 0)
                        {
                            entity.PhongThue = ((new DMPhongDAO()).GetByAll(new DMPhong(entity.PhongThueID))).FirstOrDefault();
                        }
                        else
                        {
                            entity.PhongThue = null;
                        }
                    }
                    catch
                    {
                        entity.PhongThue = null;
                    }
                    try
                    {
                        if (entity.MaPhieuThuePhong != null && entity.MaPhieuThuePhong != 0)
                        {
                            entity.ListDetail = ((new ChiTietPhieuThuePhongDAO()).GetByAll(new ChiTietPhieuThuePhong(entity.MaPhieuThuePhong, 0)));
                        }
                        else
                        {
                            entity.ListDetail = null;
                        }
                    }
                    catch
                    {
                        entity.ListDetail = null;
                    }
                    lstData.Add(entity);
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
            return lstData;
        }
        
        private string ParseListToXML(List<ChiTietPhieuThuePhong> listDetail)
        {
            string result = string.Empty;   
            try
            {
                StringBuilder sb = new StringBuilder();
                foreach (ChiTietPhieuThuePhong obj in listDetail)
                {
                    sb.AppendFormat("<obj><MaPhieuThuePhong>{0}</MaPhieuThuePhong><KhachHang>{1}</KhachHang></obj>", obj.MaPhieuThuePhong, obj.KhachHangID);
                }
                result = sb.ToString();
            }
            catch { result = string.Empty; }

            return string.IsNullOrEmpty(result) ? null : result;
        }



    }
}
